import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router } from "@angular/router"; //路由引入

@Component({
	selector: 'sa-firmware-management',
	templateUrl: './firmware-management.component.html',
})
export class FirmwareManagementComponent implements OnInit {
	tableData:any[] = []; //table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	searchKey:string = ''; //搜索keyword
	btnData:any[] = [];
	binUpload:boolean = false;
	binDeleteBatch:boolean = false;
	carUseList = carTypeListObj.carUseList
	carDeatailUseList = carTypeListObj.carDeatailUseList
	constructor(private jsonApiService: JsonApiService, private router: Router) {}

	ngOnInit() {
		this.initBtn();
		this.load();
	}
	//刷新
    refresh(){
    	this.load();
    }
	initBtn(){
		this.btnData = JSON.parse(decodeURIComponent(localStorage.getItem('cargpsw_bin:bininfo')));
		if(!this.btnData){return};
		for(let item of this.btnData){
			if(item.sourceKey === "bin:upload"&&item.parentId){
				this.binUpload = true;
			}
			if(item.sourceKey === "bin:deleteBatch"&&item.parentId){
				this.binDeleteBatch = true;
			}
		}
	}
	//表格搜索
	table_search(word) {
		this.curPage = 1
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//初始化加载list数据
	load() {
		let reqdata = 'gridPager={"pageSize":' + this.pageSize + ',"nowPage":' + this.curPage + ',"parameters":{"searchKey":"' + this.searchKey + '"}}'
		this.jsonApiService.http("bininfo/list.html", reqdata).subscribe(data => {
			for(let item of data.exhibitDatas){
				item.useTypeL1 = this.getUseTypeNameL1(item.useTypeL1);
				item.useTypeL2 = this.getUseTypeNameL2(item.useTypeL2);
			}
			this.tableData = data.exhibitDatas;
			this.totalCount = data.recordCount;
		})
	}
	getUseTypeNameL1(key){
		if(key){
			return this.carUseList[key];
		}else{
			return '';
		}
	}
	getUseTypeNameL2(key){
		if(key){
			return this.carDeatailUseList[key];
		}else{
			return '';
		}
	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}
	//新建/编辑页面
	up_bin(type) {
		this.router.navigate(['/system-config/firmware-management/firmware-management-upload'], {queryParams: {type: 'new'}});
	}
	//删除
	del_bin(row) {
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(()=>{
			let reqdata = 'ids=' + row.id;
			this.jsonApiService.http("bininfo/deleteBatch.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if(data.success){
					this.load();
				}
			})
		})
	}
}